package com.nazca.tvremote.service;

import com.nazca.tvremote.service.IEventService;
import com.nazca.tvremote.service.IEventServiceCallback;
import com.nazca.tvremote.util.RLog;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;

public class EventService extends Service {

	private static final String TAG = "EventService";
	
	protected static EventService INSTANCE;
	
    private RemoteCallbackList<IEventServiceCallback> mCallbackList =
        new RemoteCallbackList<IEventServiceCallback>();
    private IEventServiceCallback mCallback;
	
//    private EventSender mEventSender;
    private RemoteSender mRemoteSender;
    
	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		RLog.d(TAG, "onCreate()");
		if (INSTANCE == null) {
            INSTANCE = this;
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		RLog.d(TAG, "onDestroy()");
		if (INSTANCE != null) {
            INSTANCE = null;
		}
	}
	
	private final IEventService.Stub mBinder = new IEventService.Stub(){

		@Override
		public void setCallback(IEventServiceCallback cb)
				throws RemoteException {
//			Log.d(TAG, "setCallback()");
//			Log.d(TAG, "cb:" + cb);
			if (mCallback != null) {
                mCallbackList.unregister(mCallback);
            }
            mCallback = cb;
            mCallbackList.register(cb);
		}

		@Override
		public void connectToServer(String dstAddress, int dstPort)
				throws RemoteException {
			RLog.d(TAG, "connectToServer()");
			RLog.d(TAG, "dstAddress:" + dstAddress);
			RLog.d(TAG, "dstPort:" + dstPort);
//			mEventSender = EventSender.getInstance(dstAddress, dstPort);
//			boolean flag = mEventSender.connect();
//			callback().connectToServer(flag);
//			if(flag){
//				mEventSender.println("hello");
//			}
			mRemoteSender = new RemoteSender(dstAddress, dstPort);
			boolean f = mRemoteSender.connect();
			RLog.d(TAG, "f:" + f);
			if(f) mRemoteSender.println("hello");
			callback().connectToServer(f);
		}

		@Override
		public void disConnectToServer() throws RemoteException {
//			if(mEventSender != null){
//				boolean flag = false;
//				if(mEventSender.connect()){
//					mEventSender.println("bye");
//					flag = mEventSender.close();
//				}
//				callback().disConnectToServer(flag);
//			}
			if(mRemoteSender != null){
				mRemoteSender.println("bye");
				boolean f = mRemoteSender.close();
				mRemoteSender = null;
				callback().disConnectToServer(f);
			}
			callback().disConnectToServer(true);
		}

		@Override
		public void println(String line) throws RemoteException {
//			if(mEventSender.connect()){
//				mEventSender.println(line);
//			}
			if(mRemoteSender != null){
				mRemoteSender.println(line);
			}
		}
		
	};
	
	static private final IEventServiceCallback.Stub sCallbackProxy = new IEventServiceCallback.Stub(){

		@Override
		public void connectToServer(boolean successful) throws RemoteException {
			RLog.d(TAG, "connectToServer()");
			IEventServiceCallback cb = INSTANCE == null ? null: INSTANCE.mCallback;
            if (cb != null) {
                cb.connectToServer(successful);
            }
		}

		@Override
		public void disConnectToServer(boolean successful)
				throws RemoteException {
			RLog.d(TAG, "disConnectToServer()");
			IEventServiceCallback cb = INSTANCE == null ? null: INSTANCE.mCallback;
            if (cb != null) {
                cb.disConnectToServer(successful);
            }
		}
		
	};
	
	static public IEventServiceCallback callback() {
        return sCallbackProxy;
    }
	
}
